package services

import (
	"awesome-devops/applications/models"
	"awesome-devops/applications/tools"
	"awesome-devops/boot"
	"fmt"
	"gitee.com/jjawesomejj/awesome-util/config"
	"gitee.com/jjawesomejj/awesome-util/helper/commonhelper"
	"gorm.io/gorm"
	"strconv"
)

type ProjectService struct {
}

func (projectService ProjectService) Create(project models.Project) error {
	return boot.GetDbConnection().Transaction(func(tx *gorm.DB) error {

		result := tx.Create(&project)
		if result.Error != nil {
			return result.Error
		}
		gitCommand := tools.GitCommand{
			WorkDir: config.GetConfigByKey("GitProjectPath", func() interface{} {
				return "./projects/"
			}).(string),
			ProjectDir: strconv.Itoa(int(project.ID)),
			GitSshUrl:  project.GitUrl,
		}
		_, err := gitCommand.Init()
		if err != nil {
			return err
		}
		_, err = gitCommand.CheckOut(project.Branch)
		if err != nil {

		}
		return nil
	})
}

func (projectService ProjectService) CommitIds(projectId int) interface{} {
	project := models.Project{}
	boot.GetDbConnection().Where("id", projectId).First(&project)
	return project
}

func (projectService ProjectService) List(keywords string, page int, pageSize int) interface{} {
	project := models.Project{}
	query := boot.GetDbConnection().Table(project.TableName())
	if keywords != "" {
		query = query.Where("project_name like ?", commonhelper.Format(`%{}%`, keywords))
	}
	query.Table(project.TableName())
	result, err := project.Pager(query, page, pageSize, nil)
	if err != nil {
		return err
	}
	return result
}

func (projectService ProjectService) SelectList() interface{} {
	project := models.Project{}
	query := boot.GetDbConnection().Table(project.TableName())
	var list []map[string]interface{}
	result := query.Table(project.TableName()).Select("project_name,id").Find(&list)
	if result.Error != nil {
		return result.Error
	}
	return list
}

func (projectService ProjectService) CommitList(projectId int) interface{} {
	project := models.Project{}
	query := boot.GetDbConnection()
	result := query.Table(project.TableName()).Where("id", projectId).Select("*").First(&project)
	if result.Error != nil {
		return result.Error
	}
	command, err := models.GetGitCommand(project)
	if err != nil {
		return err
	}
	_, err = command.PullAll(project.Branch)
	if err != nil {
		return err
	}
	data, err := command.CommitLog()
	if err != nil {
		return err
	}
	return data
}

func (projectService ProjectService) Labels() interface{} {
	labels := make([]models.Label, 0)
	runners := make([]models.Runner, 0)
	project := models.Label{}
	query := boot.GetDbConnection()
	result := query.Table(project.TableName()).Select("*").Find(&labels)
	if result.Error != nil {
		return labels
	}
	result = boot.GetDbConnection().Table((&models.Runner{}).TableName()).Select("*").Find(&runners)
	response := make([]map[string]interface{}, 0)
	for _, label := range labels {
		itemRunner := make([]models.Runner, 0)
		fmt.Println(runners)
		for _, runner := range runners {

			if inList(int(label.Id), runner.GetRunnerLabels()) {
				itemRunner = append(itemRunner, runner)
			}
		}
		item := map[string]interface{}{
			"label":   label,
			"runners": itemRunner,
		}
		response = append(response, item)
	}
	return response
}

func inList(value int, list []int) bool {
	for _, itemValue := range list {
		if value == itemValue {
			return true
		}
	}
	return false
}
